/**
 * 
 */
package com.jinmei.mail.response;

import org.apache.commons.lang.StringUtils;

import com.jinmei.mail.bean.NewMailInfo;
import com.jinmei.mail.oauth.OAuth;
import com.jinmei.remote.response.BaseResponse;
import com.jinmei.remote.util.JSONUtils;
import com.jinmei.remote.util.RequestUtils;

/**
 * @author richard
 *
 */
public class ListeningResponse extends BaseResponse {
	
	private static final String USERNAME = "UserName";
	private static final String MAILID = "MailId";
	private static final String SENDER = "Sender";
	private static final String RECEIVER = "Receiver";
	private static final String SUBJECT = "Subject";
	private static final String SUMMARY = "Summary";
	private static final String NEWCOUNT = "NewCount";
	
    public boolean isInvalidToken() {
        return StringUtils.equals("invalid_token", getParam("error"));
    }

    public String getRet() {
        return getParam(OAuth.RET);
    }
    
    public String getVersion() {
    	return getParam(OAuth.VERSION);
    }
    
    public NewMailInfo getNewMailInfo() {
    	String mailId = getParam(MAILID);
		if (!StringUtils.isEmpty(mailId)) {
			NewMailInfo newMail = new NewMailInfo();
			newMail.setUserName(getParam(USERNAME));
			newMail.setMailId(mailId);
			newMail.setSender(getParam(SENDER));
			newMail.setReceiver(getParam(RECEIVER));
			newMail.setSubject(getParam(SUBJECT));
			newMail.setSummary(getParam(SUMMARY));
			newMail.setNewCount(getParam(NEWCOUNT));
			return newMail;
		}
		return null;
    }
    
	@Override
	protected void setBody(String body) throws Exception {
		this.body = RequestUtils.decode(body, RequestUtils.CHARSET_UTF8);
		if (!StringUtils.isEmpty(body)) {
			parameters = JSONUtils.parseJSON(body);
		}
	}

}
